package yunwo.cn.yzstation.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.zhy.autolayout.AutoLayoutActivity;
import com.zltd.decoder.Constants;
import com.zltd.decoder.DecoderManager;

import java.util.Stack;

import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.view.activity.LoginActivity;


/**
 * Created by lao on 2017/8/23.
 */

public abstract class MyBaseScanActivity extends AutoLayoutActivity implements DecoderManager.IDecoderStatusListener{
    private static Stack<Activity> listActivity = new Stack<Activity>();
    protected Typeface mTfLight;
    private TextView mTitlename;
    private ImageView mBack;
    private static final String TAG = "BaseActivity";



    //pda扫描

    protected static final int DECODE_TIMEOUT = 2100;
    protected boolean isScanTimeOut = false;
    protected DecoderManager mDecoderMgr = null;
    protected boolean isOnResume = false;
    protected int scanCase = 0;
    private static final int OPENLIGHT = 1;
    private static final int CLOSELIGHT = 2;
    //灯光控制的一个Handler
    protected static LightControlHandler lightControlHandler = null;
    protected static int RETURN_CAMERA_CONN_ERR = 0x103;


    public class LightControlHandler extends Handler {
        public void handleMessage(android.os.Message msg) {
            Log.d(TAG, "lightControlHandler msg.what = " + msg.what + " mDecoderMgr = " + mDecoderMgr);
            if(mDecoderMgr != null){
                Log.d(TAG, "FlashMode = " + mDecoderMgr.getFlashMode());
            }
            //这边是为了在非 自动补光的情境下,可以动过手动的开关灯光进行补光
            switch (msg.what) {
                case OPENLIGHT:
                    if(mDecoderMgr != null){
                        mDecoderMgr.enableLight(Constants.LOCATION_LIGHT, true);
                        if(mDecoderMgr.getFlashMode() == Constants.FLASH_ALWAYS_ON_MODE ){
                            mDecoderMgr.enableLight(Constants.FLASH_LIGHT, true);
                            mDecoderMgr.enableLight(Constants.FLOOD_LIGHT, true);
                        }
                    }
                    break;
                case CLOSELIGHT:
                    if(mDecoderMgr != null){
                        mDecoderMgr.enableLight(Constants.LOCATION_LIGHT, false);
                        if(mDecoderMgr.getFlashMode() == Constants.FLASH_ALWAYS_ON_MODE ){
                            mDecoderMgr.enableLight(Constants.FLASH_LIGHT, false);
                            mDecoderMgr.enableLight(Constants.FLOOD_LIGHT, false);
                        }
                    }
                    break;

                default:
                    break;
            }
        };
    }



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        mTfLight = Typeface.createFromAsset(getAssets(), "OpenSans-Light.ttf");
        setContentView(getLayoutRsid());
        mTitlename = (TextView) findViewById(R.id.titlename);
        mBack = (ImageView) findViewById(R.id.back);
        if (mTitlename != null) {
            mTitlename.setText(settiltile());
        }
        // 将activity推入栈中
        listActivity.push(this);
        initback();

        if(lightControlHandler == null){
            lightControlHandler = new LightControlHandler();
        }
        //mDecoderMgr.connectToDecoder();
        //1.创建DecoderManager
        mDecoderMgr = DecoderManager.getInstance();


    }

    private void initback() {
        if (mBack != null) {
            mBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    finish();
                }
            });
        }

    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 从栈中移除当前activity
        if (listActivity.contains(this)) {
            listActivity.remove(this);
        }
        mDecoderMgr = null;
    }





    protected static void finishAll() {
        int len = listActivity.size();
        for (int i = 0; i < len; i++) {
            Activity activity = listActivity.pop();
            activity.finish();
        }
    }

    protected static void finishother() {
        int len = listActivity.size();
        listActivity.remove(LoginActivity.class);
        for (int i = 0; i < len; i++) {
            Activity activity = listActivity.pop();
            activity.finish();
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        //pda
        Log.d(TAG, "onResume this=" + this);
        isOnResume = true;
        scanCase = 0;
        //2.连接扫描服务
//        int res = mDecoderMgr.connectToDecoder();
        int res = mDecoderMgr.connectDecoderSRV();

        if(res == RETURN_CAMERA_CONN_ERR){
            new AlertDialog.Builder(this)
                    .setTitle(R.string.app_name)
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setMessage(R.string.scan_message)
                    .setPositiveButton(R.string.dialog_ok, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                            closeSelf();
                        }
                    })
                    .setCancelable(false)
                    .show();
        }
        //3.为Decodermanager添加一个扫描回调接口,用来接收扫描返回数据
        mDecoderMgr.addDecoderStatusListener(this);
//       lightControlHandler.removeMessages(CLOSELIGHT);
//      lightControlHandler.sendEmptyMessage(OPENLIGHT);
        mDecoderMgr.setDataTransferType(3);

    }


    @Override
    protected void onPause() {
        super.onPause();
        isOnResume = false;
//    	lightControlHandler.sendEmptyMessageDelayed(CLOSELIGHT, 1);
        // 4.在App不工作或者退到后台之后,有必要释放资源,关闭或者移除扫描服务
        mDecoderMgr.removeDecoderStatusListener(this);
        mDecoderMgr.stopDecode();
        mDecoderMgr.disconnectDecoderSRV();
//    	mDecoderMgr.cancelDecode();
//    	mDecoderMgr.disconnectFromDecoder();
    }
    protected boolean isScanTimeOut(){
        return isScanTimeOut;
    }

    //这边由继承它的子类ScanTestActivity中已经实现



    @Override
    public void onDecoderResultChanage(String result, String time) {
        Log.d("mylog", "调用了夫类方法:"+result);
    }
    @Override
    public void onDecoderStatusChanage(int arg0) {
    }

    protected void closeSelf() {
        this.finish();
    }


    protected abstract String settiltile();
    protected abstract int getLayoutRsid();

}
